
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
 */
package be.ac.ua.comp.projectGPT.Jobs;

//~--- JDK imports ------------------------------------------------------------

import be.ac.ua.comp.projectGPT.players.Consumer;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.Vector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Generates a job.
 */
public class JobGenerator {

    final static Logger logger = LoggerFactory.getLogger(JobGenerator.class.getSimpleName());

    /**
     * Creates a job by name and parameter list.
     *
     * @param jobType The name of the job to be created.
     * @param parameters The parameters of the job.
     * @return The instantiated job.
     * @throws ClassNotFoundException 
     * @throws InstantiationException
     * @throws IllegalAccessException
     * @throws IllegalArgumentException
     * @throws NoSuchMethodException
     * @throws InvocationTargetException
     */
    public static Job createJob(Consumer consumer, String jobType, Object[] parameters)
            throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException,
                   NoSuchMethodException, InvocationTargetException {
        logger.info("Creating job");
        //Class[] classVector = new Class[20];
        Class[] classVector = new Class[parameters.length];
        logger.info(String.valueOf(parameters.length));
        if(!Arrays.equals(parameters,null)) {
            for(int i=0;i<parameters.length;i++) {
                classVector[i] = parameters[i].getClass();
            }
        }
        Job job = (Job)Class.forName(jobType).getConstructor(classVector).newInstance(parameters);
        if(job.getClass().isAssignableFrom(CallableJob.class)) {
            job.addObserver(consumer);
        }
        return job;
    }
}
